package com.udaos.modules.enroll.controllers;

import java.io.IOException;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.AppException;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.enroll.Ud_enroll;
import com.udaos.models.physical.Ud_physical_code;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.enroll.services.UdEnrollProgressService;
import com.udaos.modules.enroll.services.UdEnrollService;
import com.udaos.modules.physical.services.UdPhysicalCodeService;

/**
 * 报名WEB
 * @author mutaotao
 *
 */
@IocBean
@At("/platform/enroll")
@Filters({ @By(type = PrivateFilter.class) })
public class UdEnrollController {
	
	@Inject
	private UdEnrollService enrollService;
	@Inject
	private UdEnrollProgressService progressService;
	@Inject
	private UdPhysicalCodeService codeService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/enroll/index.html")
	@RequiresAuthentication
	public void index() {

	}
	
	/**
	 * 加載數據列表
	 * @param mobile
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full") 
	@RequiresAuthentication
	public Object data(@Param("state") Integer state,
			@Param("mobile") String mobile,
			@Param("name") String name, @Param("length") int length, @Param("start") int start,
			@Param("draw") int draw, @Param("::order") List<DataTableOrder> order,
			@Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		//获取主体
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		cnd.and("company_id", "=", company_id);
		if (!Strings.isBlank(name)){
			cnd.and("name", "like", "%" + name + "%");
		}
		if (!Strings.isBlank(mobile)){
			cnd.and("mobile", "like", "%" + mobile + "%");
		}
		if (state !=null){
			cnd.and("state", "=", state);
		}
		cnd.desc("enroll_time");
		return enrollService.data(length, start, draw, order, columns, cnd, null);
	}
	
	@At("/sendCode/?")
	@Ok("json")
	public Object sendCode(String enrollId){
		Ud_enroll enroll=enrollService.fetch(enrollId);
		if(null == enroll || !enroll.getPay_flag()){
			return Result.error("无效的订单");
		}else{
			try {
				enrollService.sendCode(enroll);
				return Result.success("发送成功");
			} catch (AppException e) {
				e.printStackTrace();
			}
		}
		return enroll;
	}
	@At("/checkIsEnroll/?")
	@Ok("json")
	public Object checkIsEnroll(String enrollId){
		try {
			return enrollService.checkIsEnroll(enrollId);
		} catch (IOException e) {
			e.printStackTrace();
			return Result.error("调用微信接口失败，请稍后操作");
		}
	}

	@At
	@Ok("json") 
	@RequiresAuthentication
	public Object getPhysical(String id){
		//查找是否已发送
		Ud_physical_code code=codeService.fetch(Cnd.where("bill_id", "=", id).and("bill_type", "=", "enroll"));
		if(code !=null){
			return code.getSn()+"-"+code.getCode();
		}
		return "";
	}
}
